﻿
using Models.Common;
using Models.Device;
using UtilsBase;
using Microsoft.EntityFrameworkCore;
using NewLife;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    ///普锐森社 RS485-PH检测 液体PR-*-PH-N01 温度 带温补
    /// </summary>
    public class ServiceDeviceRS4851014_PH
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //回调
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;


                if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                {
                    return;
                }

                //地址要一样才接收
                if (modelDeviceData.commandResults[0].value.Substring(0, 2) != commandLogModel.content.Substring(0, 2))
                {
                    return;
                }

                if (commandLogModel.key == "collect")
                {

                    //普锐森社 RS485-PH检测 液体PR-*-PH-N01 温度
                    //https://detail.tmall.com/item.htm?app=chrome&bxsign=scdZ9JpKY7KITx1r7c-sxVPULHKt39WeEXJKm_UD_AzFM7E6_9VCZILEVzj19F0Cz9jcjcLzoK41-epEbZnHlE2vjdvGiRD2dcq9azHggQNKYip69L3CaDIBD2XWwio5SSN&cpp=1&id=653395140353&share_crt_v=1&shareurl=true&short_name=h.Urta6qn&sourceType=item&sp_abtk=gray_1_code_simpleAndroid2&sp_tk=Wk1nd2RRM2ZqZHM=&spm=a2159r.13376460.0.0&tbSocialPopKey=shareItem&tk=ZMgwdQ3fjds%20CZ0001&ttid=1568860058617@taobao_android_10.22.10&un=87d1c8dd838bb7dde7c4cca5375f99dd&un_site=0&ut_sk=1.YSw0382wAckDAP1SKlIXnY+4_21646297_1679384494095.GoodsTitleURL.1
                    //pH 计算：316H（十六进制）= 790 =>pH = 7.90
                    string result = modelDeviceData.commandResults[0].value;
                    //03 03 04 02 C5 01 EF 88 6A
                    if (result.Length != 26)
                    {
                        return;
                    }

                    string dataHex = result.Substring(9, 5);
                    double ph = new UtilsDataTypeChange().hex16Convert10(dataHex) * 0.01;
                    //Console.WriteLine("PH：" + ph);


                    //温度
                    dataHex = result.Substring(15, 5);
                    //double temperature = new UtilsDataTypeChange().hex16Convert10(dataHex) * 0.1;


                    string temperatureBinary = UtilsDataTypeChange.hexToBinaryTolStr(dataHex);
                    double temperature = 0.0;
                    if (temperatureBinary.Substring(0, 1) == "0")
                    {
                        temperature = new UtilsDataTypeChange().hex16Convert10(dataHex) * 0.1;
                    }
                    else if (temperatureBinary.Substring(0, 1) == "1")
                    {
                        //    
                        //"FF 9B" 转二进制1111111110011011
                        //1111111110011011 反码 
                        //0000000001100100
                        //补码最后一位加1
                        //0000000001100101
                        //=1100101
                        string bumabinary = UtilsByte.GetBuMaBinary(temperatureBinary);
                        temperature = UtilsDataTypeChange.binaryToDecimalStr(bumabinary) * -0.1;
                    }




                    //Console.WriteLine("temperature：" + temperature.ToString() + "℃");
                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    //Dictionary<string, double> keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, double>>(modelDeviceData.content);
                    //写日志
                    log.writeSensorlog(commandLogModel.d_guid, "ph", UtilsNumber.GetNumberForLength(ph, 1).ToString(), "PH值", result);
                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(temperature, 1).ToString(), "温度", result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
